import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import { CombinedState } from 'reducers';
import { JobStatusEnum } from 'typings/annotion/task';

export default function useAnnotation() {
    const approval = useSelector((state: CombinedState) => state.annotation.approval);
    const firstReview = useSelector((state: CombinedState) => state.annotation.firstReview);
    const [origin = {}, reviewData = {}] = firstReview.activatedData ?? [];
    // 获取任务状态
    const jobStatus = useSelector((state: CombinedState) => state.annotation.approval.taskImageInfo.subTaskStatus);

    const readonly = useMemo(
        () => ![JobStatusEnum.WAITING_ANNOTATION, JobStatusEnum.FIRST_REVIEW_REJECTED].includes(jobStatus),
        [jobStatus],
    );
    /**
     * 对比标签和属性是否有修改
     */
    function differenceObj(obj: any, obj1: any): boolean | string[] {
        if (Object.keys(obj).length === 0) return false;
        const {
            label: { id: labelId },
            attributes,
        } = obj;
        const {
            label: { id: labelId1 },
            attributes: attributes1,
        } = obj1;
        if (labelId !== labelId1) {
            return true;
        }
        const diffentAttr = Object.keys(attributes).filter(
            (key: string): boolean => attributes?.[key] !== attributes1?.[key],
        );
        return diffentAttr.length ? diffentAttr : false;
    }
    return {
        approval,
        jobStatus,
        firstReview,
        origin,
        reviewData,
        readonly,
        differenceObj,
    };
}
